Conditions | 66 |
Total Lines | 294 |
Code Lines | 174 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like LAB.src.js ➔ create_sandbox often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*! LAB.js (LABjs :: Loading And Blocking JavaScript) |
||
196 | function create_sandbox() { |
||
197 | var global_defaults = {}, |
||
198 | can_use_preloading = real_preloading || xhr_or_cache_preloading, |
||
199 | queue = [], |
||
200 | registry = {}, |
||
201 | instanceAPI |
||
202 | ; |
||
203 | |||
204 | // global defaults |
||
205 | global_defaults[_UseLocalXHR] = true; |
||
206 | global_defaults[_AlwaysPreserveOrder] = false; |
||
207 | global_defaults[_AllowDuplicates] = false; |
||
208 | global_defaults[_CacheBust] = false; |
||
209 | /*!START_DEBUG*/global_defaults[_Debug] = false;/*!END_DEBUG*/ |
||
210 | global_defaults[_BasePath] = ""; |
||
211 | |||
212 | // execute a script that has been preloaded already |
||
213 | function execute_preloaded_script(chain_opts,script_obj,registry_item) { |
||
214 | var script; |
||
215 | |||
216 | function preload_execute_finished() { |
||
217 | if (script != null) { // make sure this only ever fires once |
||
218 | script = null; |
||
219 | script_executed(registry_item); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | if (registry[script_obj.src].finished) return; |
||
224 | if (!chain_opts[_AllowDuplicates]) registry[script_obj.src].finished = true; |
||
225 | |||
226 | script = registry_item.elem || document.createElement("script"); |
||
227 | if (script_obj.type) script.type = script_obj.type; |
||
228 | if (script_obj.charset) script.charset = script_obj.charset; |
||
229 | create_script_load_listener(script,registry_item,"finished",preload_execute_finished); |
||
230 | |||
231 | // script elem was real-preloaded |
||
232 | if (registry_item.elem) { |
||
233 | registry_item.elem = null; |
||
234 | } |
||
235 | // script was XHR preloaded |
||
236 | else if (registry_item.text) { |
||
237 | script.onload = script.onreadystatechange = null; // script injection doesn't fire these events |
||
238 | script.text = registry_item.text; |
||
239 | } |
||
240 | // script was cache-preloaded |
||
241 | else { |
||
242 | script.src = script_obj.real_src; |
||
243 | } |
||
244 | append_to.insertBefore(script,append_to.firstChild); |
||
245 | |||
246 | // manually fire execution callback for injected scripts, since events don't fire |
||
247 | if (registry_item.text) { |
||
248 | preload_execute_finished(); |
||
249 | } |
||
250 | } |
||
251 | |||
252 | // process the script request setup |
||
253 | function do_script(chain_opts,script_obj,chain_group,preload_this_script) { |
||
254 | var registry_item, |
||
255 | registry_items, |
||
256 | ready_cb = function(){ script_obj.ready_cb(script_obj,function(){ execute_preloaded_script(chain_opts,script_obj,registry_item); }); }, |
||
257 | finished_cb = function(){ script_obj.finished_cb(script_obj,chain_group); } |
||
258 | ; |
||
259 | |||
260 | script_obj.src = canonical_uri(script_obj.src,chain_opts[_BasePath]); |
||
261 | script_obj.real_src = script_obj.src + |
||
262 | // append cache-bust param to URL? |
||
263 | (chain_opts[_CacheBust] ? ((/\?.*$/.test(script_obj.src) ? "&_" : "?_") + ~~(Math.random()*1E9) + "=") : "") |
||
264 | ; |
||
265 | |||
266 | if (!registry[script_obj.src]) registry[script_obj.src] = {items:[],finished:false}; |
||
267 | registry_items = registry[script_obj.src].items; |
||
268 | |||
269 | // allowing duplicates, or is this the first recorded load of this script? |
||
270 | if (chain_opts[_AllowDuplicates] || registry_items.length == 0) { |
||
271 | registry_item = registry_items[registry_items.length] = { |
||
272 | ready:false, |
||
273 | finished:false, |
||
274 | ready_listeners:[ready_cb], |
||
275 | finished_listeners:[finished_cb] |
||
276 | }; |
||
277 | |||
278 | request_script(chain_opts,script_obj,registry_item, |
||
279 | // which callback type to pass? |
||
280 | ( |
||
281 | (preload_this_script) ? // depends on script-preloading |
||
282 | function(){ |
||
283 | registry_item.ready = true; |
||
284 | for (var i=0; i<registry_item.ready_listeners.length; i++) { |
||
285 | registry_item.ready_listeners[i](); |
||
286 | } |
||
287 | registry_item.ready_listeners = []; |
||
288 | } : |
||
289 | function(){ script_executed(registry_item); } |
||
290 | ), |
||
291 | // signal if script-preloading should be used or not |
||
292 | preload_this_script |
||
293 | ); |
||
294 | } |
||
295 | else { |
||
296 | registry_item = registry_items[0]; |
||
297 | if (registry_item.finished) { |
||
298 | finished_cb(); |
||
299 | } |
||
300 | else { |
||
301 | registry_item.finished_listeners.push(finished_cb); |
||
302 | } |
||
303 | } |
||
304 | } |
||
305 | |||
306 | // creates a closure for each separate chain spawned from this $LAB instance, to keep state cleanly separated between chains |
||
307 | function create_chain() { |
||
308 | var chainedAPI, |
||
309 | chain_opts = merge_objs(global_defaults,{}), |
||
310 | chain = [], |
||
311 | exec_cursor = 0, |
||
312 | scripts_currently_loading = false, |
||
313 | group |
||
314 | ; |
||
315 | |||
316 | // called when a script has finished preloading |
||
317 | function chain_script_ready(script_obj,exec_trigger) { |
||
318 | /*!START_DEBUG*/if (chain_opts[_Debug]) log_msg("script preload finished: "+script_obj.real_src);/*!END_DEBUG*/ |
||
319 | script_obj.ready = true; |
||
320 | script_obj.exec_trigger = exec_trigger; |
||
321 | advance_exec_cursor(); // will only check for 'ready' scripts to be executed |
||
322 | } |
||
323 | |||
324 | // called when a script has finished executing |
||
325 | function chain_script_executed(script_obj,chain_group) { |
||
326 | /*!START_DEBUG*/if (chain_opts[_Debug]) log_msg("script execution finished: "+script_obj.real_src);/*!END_DEBUG*/ |
||
327 | script_obj.ready = script_obj.finished = true; |
||
328 | script_obj.exec_trigger = null; |
||
329 | // check if chain group is all finished |
||
330 | for (var i=0; i<chain_group.scripts.length; i++) { |
||
331 | if (!chain_group.scripts[i].finished) return; |
||
332 | } |
||
333 | // chain_group is all finished if we get this far |
||
334 | chain_group.finished = true; |
||
335 | advance_exec_cursor(); |
||
336 | } |
||
337 | |||
338 | // main driver for executing each part of the chain |
||
339 | function advance_exec_cursor() { |
||
340 | while (exec_cursor < chain.length) { |
||
341 | if (is_func(chain[exec_cursor])) { |
||
342 | /*!START_DEBUG*/if (chain_opts[_Debug]) log_msg("$LAB.wait() executing: "+chain[exec_cursor]);/*!END_DEBUG*/ |
||
343 | try { chain[exec_cursor++](); } catch (err) { |
||
344 | /*!START_DEBUG*/if (chain_opts[_Debug]) log_error("$LAB.wait() error caught: ",err);/*!END_DEBUG*/ |
||
345 | } |
||
346 | continue; |
||
347 | } |
||
348 | else if (!chain[exec_cursor].finished) { |
||
349 | if (check_chain_group_scripts_ready(chain[exec_cursor])) continue; |
||
350 | break; |
||
351 | } |
||
352 | exec_cursor++; |
||
353 | } |
||
354 | // we've reached the end of the chain (so far) |
||
355 | if (exec_cursor == chain.length) { |
||
356 | scripts_currently_loading = false; |
||
357 | group = false; |
||
358 | } |
||
359 | } |
||
360 | |||
361 | // setup next chain script group |
||
362 | function init_script_chain_group() { |
||
363 | if (!group || !group.scripts) { |
||
364 | chain.push(group = {scripts:[],finished:true}); |
||
365 | } |
||
366 | } |
||
367 | |||
368 | // API for $LAB chains |
||
369 | chainedAPI = { |
||
370 | // start loading one or more scripts |
||
371 | script:function(){ |
||
372 | for (var i=0; i<arguments.length; i++) { |
||
373 | (function(script_obj,script_list){ |
||
374 | var splice_args; |
||
375 | |||
376 | if (!is_array(script_obj)) { |
||
377 | script_list = [script_obj]; |
||
378 | } |
||
379 | for (var j=0; j<script_list.length; j++) { |
||
380 | init_script_chain_group(); |
||
381 | script_obj = script_list[j]; |
||
382 | |||
383 | if (is_func(script_obj)) script_obj = script_obj(); |
||
384 | if (!script_obj) continue; |
||
385 | if (is_array(script_obj)) { |
||
386 | // set up an array of arguments to pass to splice() |
||
387 | splice_args = [].slice.call(script_obj); // first include the actual array elements we want to splice in |
||
388 | splice_args.unshift(j,1); // next, put the `index` and `howMany` parameters onto the beginning of the splice-arguments array |
||
389 | [].splice.apply(script_list,splice_args); // use the splice-arguments array as arguments for splice() |
||
390 | j--; // adjust `j` to account for the loop's subsequent `j++`, so that the next loop iteration uses the same `j` index value |
||
391 | continue; |
||
392 | } |
||
393 | if (typeof script_obj == "string") script_obj = {src:script_obj}; |
||
394 | script_obj = merge_objs(script_obj,{ |
||
395 | ready:false, |
||
396 | ready_cb:chain_script_ready, |
||
397 | finished:false, |
||
398 | finished_cb:chain_script_executed |
||
399 | }); |
||
400 | group.finished = false; |
||
401 | group.scripts.push(script_obj); |
||
402 | |||
403 | do_script(chain_opts,script_obj,group,(can_use_preloading && scripts_currently_loading)); |
||
404 | scripts_currently_loading = true; |
||
405 | |||
406 | if (chain_opts[_AlwaysPreserveOrder]) chainedAPI.wait(); |
||
407 | } |
||
408 | })(arguments[i],arguments[i]); |
||
409 | } |
||
410 | return chainedAPI; |
||
411 | }, |
||
412 | // force LABjs to pause in execution at this point in the chain, until the execution thus far finishes, before proceeding |
||
413 | wait:function(){ |
||
414 | if (arguments.length > 0) { |
||
415 | for (var i=0; i<arguments.length; i++) { |
||
416 | chain.push(arguments[i]); |
||
417 | } |
||
418 | group = chain[chain.length-1]; |
||
419 | } |
||
420 | else group = false; |
||
421 | |||
422 | advance_exec_cursor(); |
||
423 | |||
424 | return chainedAPI; |
||
425 | } |
||
426 | }; |
||
427 | |||
428 | // the first chain link API (includes `setOptions` only this first time) |
||
429 | return { |
||
430 | script:chainedAPI.script, |
||
431 | wait:chainedAPI.wait, |
||
432 | setOptions:function(opts){ |
||
433 | merge_objs(opts,chain_opts); |
||
434 | return chainedAPI; |
||
435 | } |
||
436 | }; |
||
437 | } |
||
438 | |||
439 | // API for each initial $LAB instance (before chaining starts) |
||
440 | instanceAPI = { |
||
441 | // main API functions |
||
442 | setGlobalDefaults:function(opts){ |
||
443 | merge_objs(opts,global_defaults); |
||
444 | return instanceAPI; |
||
445 | }, |
||
446 | setOptions:function(){ |
||
447 | return create_chain().setOptions.apply(null,arguments); |
||
448 | }, |
||
449 | script:function(){ |
||
450 | return create_chain().script.apply(null,arguments); |
||
451 | }, |
||
452 | wait:function(){ |
||
453 | return create_chain().wait.apply(null,arguments); |
||
454 | }, |
||
455 | |||
456 | // built-in queuing for $LAB `script()` and `wait()` calls |
||
457 | // useful for building up a chain programmatically across various script locations, and simulating |
||
458 | // execution of the chain |
||
459 | queueScript:function(){ |
||
460 | queue[queue.length] = {type:"script", args:[].slice.call(arguments)}; |
||
461 | return instanceAPI; |
||
462 | }, |
||
463 | queueWait:function(){ |
||
464 | queue[queue.length] = {type:"wait", args:[].slice.call(arguments)}; |
||
465 | return instanceAPI; |
||
466 | }, |
||
467 | runQueue:function(){ |
||
468 | var $L = instanceAPI, len=queue.length, i=len, val; |
||
469 | for (;--i>=0;) { |
||
470 | val = queue.shift(); |
||
471 | $L = $L[val.type].apply(null,val.args); |
||
472 | } |
||
473 | return $L; |
||
474 | }, |
||
475 | |||
476 | // rollback `[global].$LAB` to what it was before this file was loaded, the return this current instance of $LAB |
||
477 | noConflict:function(){ |
||
478 | global.$LAB = _$LAB; |
||
479 | return instanceAPI; |
||
480 | }, |
||
481 | |||
482 | // create another clean instance of $LAB |
||
483 | sandbox:function(){ |
||
484 | return create_sandbox(); |
||
485 | } |
||
486 | }; |
||
487 | |||
488 | return instanceAPI; |
||
489 | } |
||
490 | |||
514 | })(this); |